/*
 * Created by JFormDesigner on Fri Jun 16 18:18:06 CST 2023
 */

package fm.supplier;

import dao.Dao_supplier;
import model.Supplier;

import java.awt.*;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class UpSupplier extends JInternalFrame {

    String [] columns = {"供应商编号","供应商","联系人","联系电话","地址"};

    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columns.length];

        for(int i = 0; i < list.size(); i++)
        {
            Supplier supplier = (Supplier) list.get(i);

            results[i][0] = supplier.getSupplierID();
            results[i][1] = supplier.getSupplierName();
            results[i][2] = supplier.getSupplierPeople();
            results[i][3] = supplier.getSupplierPhone();
            results[i][4] = supplier.getAddress();
        }

        return results;
    }

    public UpSupplier() {
        initComponents();
        iniDate();
    }

    public void iniDate()
    {
        Object [][] results = getFS(Dao_supplier.selectAll());
        DefaultTableModel model = new DefaultTableModel(results, columns);
        table.setModel(model);
    }

    private void cancel(ActionEvent e) {
        // TODO add your code here
        doDefaultCloseAction();
    }

    private void tableMouseClicked(MouseEvent e) {
        int setRow = table.getSelectedRow();
        if(setRow == -1) return;

        String s_id = table.getValueAt(setRow,0).toString().trim();
        String s_name = table.getValueAt(setRow,1).toString().trim();
        String man = table.getValueAt(setRow,2).toString().trim();
        String phone = table.getValueAt(setRow,3).toString().trim();
        String address = table.getValueAt(setRow,4).toString().trim();

        supplierIDTextField.setText(s_id);
        supplierTextField.setText(s_name);
        manTextField.setText(man);
        phoneTextField.setText(phone);
        addressTextField.setText(address);

    }


    private void update(ActionEvent e) {
        if(supplierTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入供应商名称！");
            return;
        }

        if(manTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入联系人！");
            return;
        }

        if(phoneTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入联系电话！");
            return;
        }

        if(addressTextField.getText().length() == 0)
        {
            JOptionPane.showMessageDialog(null,"请输入地址！");
            return;
        }

        String supplierID = supplierIDTextField.getText().trim();
        String supplier = supplierTextField.getText().trim();
        String man = manTextField.getText().trim();
        String phone = phoneTextField.getText().trim();
        String address = addressTextField.getText().trim();

        int i = Dao_supplier.update(supplierID, supplier, phone, man, address);

        if(i == 1)
        {
            JOptionPane.showMessageDialog(null,"修改成功！");
            Object [][] results = getFS(Dao_supplier.selectAll());
            DefaultTableModel tableModel = new DefaultTableModel(results,columns);
            table.setModel(tableModel);

            doDefaultCloseAction();
        }
    }

    private void delete(ActionEvent e) {
        String s_id = supplierIDTextField.getText().trim();

        int i = Dao_supplier.delete(s_id);

        if (i == 1)
        {
            JOptionPane.showMessageDialog(null,"删除成功！");
            Object [][] results = getFS(Dao_supplier.selectAll());
            DefaultTableModel tableModel = new DefaultTableModel(results,columns);
            table.setModel(tableModel);

            doDefaultCloseAction();
        }
    }




    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        updateButton = new JButton();
        deleteButton = new JButton();
        cancelButton = new JButton();
        panel2 = new JPanel();
        scrollPane1 = new JScrollPane();
        table = new JTable();
        panel3 = new JPanel();
        label1 = new JLabel();
        supplierIDTextField = new JTextField();
        supplierTextField = new JTextField();
        label2 = new JLabel();
        label3 = new JLabel();
        manTextField = new JTextField();
        label4 = new JLabel();
        addressTextField = new JTextField();
        label5 = new JLabel();
        phoneTextField = new JTextField();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("\u4f9b\u5e94\u5546\u4fee\u6539\u4e0e\u5220\u9664");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setPreferredSize(new Dimension(412, 50));
            panel1.setLayout(null);

            //---- updateButton ----
            updateButton.setText("\u786e\u8ba4");
            updateButton.addActionListener(e -> update(e));
            panel1.add(updateButton);
            updateButton.setBounds(122, 15, 55, updateButton.getPreferredSize().height);

            //---- deleteButton ----
            deleteButton.setText("\u5220\u9664");
            deleteButton.addActionListener(e -> delete(e));
            panel1.add(deleteButton);
            deleteButton.setBounds(new Rectangle(new Point(227, 15), deleteButton.getPreferredSize()));

            //---- cancelButton ----
            cancelButton.setText("\u53d6\u6d88");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(new Rectangle(new Point(332, 14), cancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 324, 518, panel1.getPreferredSize().height);

        //======== panel2 ========
        {
            panel2.setPreferredSize(new Dimension(0, 180));
            panel2.setBorder(new TitledBorder("\u4f9b\u5e94\u5546\u4fe1\u606f"));
            panel2.setLayout(null);

            //======== scrollPane1 ========
            {

                //---- table ----
                table.addMouseListener(new MouseAdapter() {
                    @Override
                    public void mouseClicked(MouseEvent e) {
                        tableMouseClicked(e);
                    }
                });
                scrollPane1.setViewportView(table);
            }
            panel2.add(scrollPane1);
            scrollPane1.setBounds(7, 20, 504, 150);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel2.getComponentCount(); i++) {
                    Rectangle bounds = panel2.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel2.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel2.setMinimumSize(preferredSize);
                panel2.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel2);
        panel2.setBounds(0, 0, 518, panel2.getPreferredSize().height);

        //======== panel3 ========
        {
            panel3.setLayout(null);

            //---- label1 ----
            label1.setText("\u4f9b\u5e94\u5546\u7f16\u53f7\uff1a");
            panel3.add(label1);
            label1.setBounds(49, 20, label1.getPreferredSize().width, 19);

            //---- supplierIDTextField ----
            supplierIDTextField.setEditable(false);
            panel3.add(supplierIDTextField);
            supplierIDTextField.setBounds(119, 15, 95, supplierIDTextField.getPreferredSize().height);

            //---- supplierTextField ----
            supplierTextField.setBackground(Color.white);
            panel3.add(supplierTextField);
            supplierTextField.setBounds(355, 16, 110, supplierTextField.getPreferredSize().height);

            //---- label2 ----
            label2.setText("\u4f9b\u5e94\u5546\uff1a");
            panel3.add(label2);
            label2.setBounds(305, 18, label2.getPreferredSize().width, 20);

            //---- label3 ----
            label3.setText("\u8054\u7cfb\u4eba\uff1a");
            panel3.add(label3);
            label3.setBounds(73, 76, label3.getPreferredSize().width, 18);
            panel3.add(manTextField);
            manTextField.setBounds(120, 71, 94, manTextField.getPreferredSize().height);

            //---- label4 ----
            label4.setText("\u5730\u5740\uff1a");
            panel3.add(label4);
            label4.setBounds(317, 74, label4.getPreferredSize().width, 19);
            panel3.add(addressTextField);
            addressTextField.setBounds(358, 69, 107, addressTextField.getPreferredSize().height);

            //---- label5 ----
            label5.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
            panel3.add(label5);
            label5.setBounds(170, 115, label5.getPreferredSize().width, 23);
            panel3.add(phoneTextField);
            phoneTextField.setBounds(230, 112, 125, phoneTextField.getPreferredSize().height);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel3.getComponentCount(); i++) {
                    Rectangle bounds = panel3.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel3.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel3.setMinimumSize(preferredSize);
                panel3.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel3);
        panel3.setBounds(0, 180, 518, 144);

        contentPane.setPreferredSize(new Dimension(525, 405));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton updateButton;
    private JButton deleteButton;
    private JButton cancelButton;
    private JPanel panel2;
    private JScrollPane scrollPane1;
    private JTable table;
    private JPanel panel3;
    private JLabel label1;
    private JTextField supplierIDTextField;
    private JTextField supplierTextField;
    private JLabel label2;
    private JLabel label3;
    private JTextField manTextField;
    private JLabel label4;
    private JTextField addressTextField;
    private JLabel label5;
    private JTextField phoneTextField;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
